package com.shanghaionstar.nav;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.weight.CustomBtnDrawableRightCenter;
import com.onstar.cn.weight.CustomImageView;
import com.onstar.cn.weight.DeleteEditText;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.VehicleLoadingActivity;
import com.shanghaionstar.nav.adapter.NavMoreIssuedHistoryAdapter;
import com.shanghaionstar.nav.iview.INavMoreIssuedHistoryView;
import com.shanghaionstar.nav.model.PoiHistoryInfo;
import com.shanghaionstar.nav.presenter.NavMoreIssuedHistoryPresenter;
import com.shanghaionstar.nav.service.AbstractMapActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * @author jun chen
 * @ClassName: NavMoreIssuedHistoryFragment
 * @date 2016年1月26日 下午1:28:37
 * @Description: 更多下发历史记录页面
 */
public class NavMoreIssuedHistoryFragment extends BaseFragment implements INavMoreIssuedHistoryView {

    private AbstractMapActivity mapActivity;
    private LogManager log = LogManager.getInstance(getActivity());
    @FindViewById
    private RelativeLayout frame_back;
    @FindViewById(R.id.edit_search_addressinfo)
    private DeleteEditText edit_search_addressinfo;
    @FindViewById(R.id.right_btn)
    private CustomImageView right_btn;

    @FindViewById(R.id.listview_more_issued_history)
    private ListView listview_more_issued_history;
    @FindViewById(R.id.no_list_data)
    private TextView no_list_data;
    @FindViewById(R.id.btn_bottom_more_history_record)
    private CustomBtnDrawableRightCenter btn_bottom_more_history_record;
    private NavMoreIssuedHistoryAdapter mAdapter;
    private List<PoiHistoryInfo> historyList = new ArrayList<PoiHistoryInfo>();
    private NavMoreIssuedHistoryPresenter navMoreIssuedHistoryPresenter;
    private FragmentManager fragmentManager;
    @FindViewById
    private View navbar_head_view;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isFirstLoad()) {
            navMoreIssuedHistoryPresenter = new NavMoreIssuedHistoryPresenter(mapActivity.getScheduler(), mapActivity);
            navMoreIssuedHistoryPresenter.setView(this);
            navMoreIssuedHistoryPresenter.getTBTHistory();
            edit_search_addressinfo.setFocusable(false);
            btn_bottom_more_history_record.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mapActivity.removeCurrentAndShowPrevFragment(fragmentManager, NavMoreIssuedHistoryFragment.class);
                }
            });
            navbar_head_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mapActivity.removeCurrentAndShowPrevFragment(fragmentManager, NavMoreIssuedHistoryFragment.class);
                }
            });

            edit_search_addressinfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goBack();
                }
            });
            frame_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goBack();
                }
            });
        }
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mapActivity = (AbstractMapActivity) activity;
        fragmentManager = mapActivity.getSupportFragmentManager();
    }

    @Override
    public int getLayoutId() {
        return R.layout.nav_more_issued_by_history;
    }


    @Override
    public void setLoading(boolean isLoading) {

    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(Object object) {

    }

    @Override
    public Activity onActivity() {
        return null;
    }

    @Override
    public void showTBTHistory(final List<PoiHistoryInfo> tbtHistory) {
        if (tbtHistory == null || tbtHistory.size() == 0) {
            no_list_data.setVisibility(View.VISIBLE);
            no_list_data.setText(mapActivity.getResources().getString(R.string.no_navigtion_history));
        } else {
            no_list_data.setVisibility(View.GONE);
        }
        if (mAdapter == null) {
            mAdapter = new NavMoreIssuedHistoryAdapter(tbtHistory, mapActivity, navMoreIssuedHistoryPresenter);
            listview_more_issued_history.setAdapter(mAdapter);
            listview_more_issued_history.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if(tbtHistory !=null && tbtHistory.size()>0) {
                        if(tbtHistory.get(position)!=null) {
                            navMoreIssuedHistoryPresenter.onItemClick(tbtHistory.get(position).getPoiItemInfo());
                        }
                    }
                }
            });
        } else {
            mAdapter.setList(tbtHistory);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showCommandLoading(boolean isShow) {
        if(isShow) {
            startActivity(new Intent(mapActivity, VehicleLoadingActivity.class));
        }
    }
}
